package edu.avans.hartigehap.service.jpa;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import edu.avans.hartigehap.domain.ActualWebCustomer;
import edu.avans.hartigehap.domain.AuthenticationSecurity;
import edu.avans.hartigehap.domain.StateException;
import edu.avans.hartigehap.domain.WebCustomer;
import edu.avans.hartigehap.repository.WebSecurityRepository;
import edu.avans.hartigehap.service.WebSecurityService;

@Service("webSecurityService")
@Repository
@Transactional(rollbackFor = StateException.class)
public class WebSecurityServiceImpl implements WebSecurityService{

	@Autowired
	private WebSecurityRepository webSecurityRepo;

	@Transactional
	public WebCustomer login(String username, String password) {
		List<ActualWebCustomer> accounts = Lists.newArrayList(webSecurityRepo.findAll());
		AuthenticationSecurity as = new AuthenticationSecurity(username, password, accounts);
		as.setLoggedIn(true);
		return as;
	}

	@Transactional
	public void register(String username, String password) {

	}

	@Transactional
	public void createAccount(String username, String password, String address,
			String phoneNumber, String emailaddress) {
		ActualWebCustomer account = new ActualWebCustomer(username, password, address, phoneNumber, emailaddress);
		webSecurityRepo.save(account);
	}

	@Transactional
	public void createUserAccount(String username, String password,
			String firstName, String lastName, Date birthDate,
			String address, String phoneNumber, String emailaddress) {
		ActualWebCustomer account = new ActualWebCustomer(username, password, firstName, lastName, birthDate, address, phoneNumber, emailaddress);
		webSecurityRepo.save(account);
	}

}
